/**
 * Copyright(c)2012 Beijing PeaceMap Co.,Ltd.
 * All right reserved. 
 */
package com.pmc.dwa.security.model;

import java.util.Collection;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @description 
 * @author aokunsang
 * @date 2013-1-6
 */
public class TUserDetails extends TSection implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String userid;      //用户id
	private String username;   //用户名(唯一值)
	private String password;   //密码
	private String truename;    //真实姓名
	private String ustatus;    //是否启用（1-启用、0-禁用）
	
	private Set<GrantedAuthority> authorities;
	private boolean accountNonExpired = true;  //账号是否没有过期
	private boolean accountNonLocked = true;      //账号是否没有锁定
	private boolean credentialsNonExpired = true;  //证书是否过期
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return this.authorities;
	}
	@Override
	public String getPassword() {
		return this.password;
	}
	@Override
	public String getUsername() {
		return this.username;
	}
	@Override
	public boolean isAccountNonExpired() {
		return this.accountNonExpired;
	}
	@Override
	public boolean isAccountNonLocked() {
		return this.accountNonLocked;
	}
	@Override
	public boolean isCredentialsNonExpired() {
		return this.credentialsNonExpired;
	}
	@Override
	public boolean isEnabled() {
		return ustatus != null && "1".equals(ustatus) ? true : false;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @param authorities the authorities to set
	 */
	public void setAuthorities(Set<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	/**
	 * @return the userid
	 */
	public String getUserid() {
		return userid;
	}
	/**
	 * @param userid the userid to set
	 */
	public void setUserid(String userid) {
		this.userid = userid;
	}
	/**
	 * @return the ustatus
	 */
	public String getUstatus() {
		return ustatus;
	}
	/**
	 * @param ustatus the ustatus to set
	 */
	public void setUstatus(String ustatus) {
		this.ustatus = ustatus;
	}
	/* 解决一个账号同时登录问题；参考http://blog.csdn.net/sinlff/article/details/5892531*/
	@Override
	public int hashCode() {
		return username.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		if(obj.getClass() == TUserDetails.class){
			TUserDetails user = (TUserDetails)obj;
			return this.username.equals(user.getUsername());
		}
		return false;
	}
	/**
	 * @return the truename
	 */
	public String getTruename() {
		return truename;
	}
	/**
	 * @param truename the truename to set
	 */
	public void setTruename(String truename) {
		this.truename = truename;
	}
}
